package com.qn.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qn.entity.User;
import com.qn.entity.dto.UserDTO;
import com.qn.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {

    private final UserService userService;

    @GetMapping("/list")
    public List<UserDTO> list() {
        List<User> list = userService.getList();
        return BeanUtil.copyToList(list, UserDTO.class);
    }

    @GetMapping("/get")
    public UserDTO get(Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return null;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        return userDTO;
    }

}
